from flask import Blueprint, request, jsonify
from flask_jwt_extended import jwt_required, get_jwt_identity
from controllers.sentence_controller import SentenceController

sentences_bp = Blueprint('sentences', __name__)

@sentences_bp.route('', methods=['GET'])
@jwt_required()
def get_sentences():
    """获取所有句子（原有功能，保持兼容性）"""
    search_keyword = request.args.get('search', '')
    if search_keyword:
        result = SentenceController.search_sentences(search_keyword)
    else:
        result = SentenceController.get_all_sentences()
    return jsonify(result)

# ==================== 用户分配句子相关接口 ====================

@sentences_bp.route('/assigned', methods=['GET'])
@jwt_required()
def get_assigned_sentences():
    """获取用户分配的句子列表（按顺序）"""
    current_user = get_jwt_identity()
    user_id = int(current_user)

    result = SentenceController.get_user_assigned_sentences(user_id)
    return jsonify(result)

@sentences_bp.route('/next', methods=['GET'])
@jwt_required()
def get_next_sentence():
    """获取下一个待录制的句子"""
    current_user = get_jwt_identity()
    user_id = int(current_user)

    result = SentenceController.get_next_unrecorded_sentence(user_id)
    return jsonify(result)

@sentences_bp.route('/progress', methods=['GET'])
@jwt_required()
def get_sentence_progress():
    """获取用户句子录制进度"""
    current_user = get_jwt_identity()
    user_id = int(current_user)

    result = SentenceController.get_user_sentence_progress(user_id)
    return jsonify(result)